/**
 * <p><b>Title:</b> LiveBillManager.java </p>
 *
 * @Package com.wondertek.oes.charge.service.manager
 */
package com.wondertek.poms.sync.manager.main;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.ProductInfoPackage;
import com.wondertek.poms.dao.po.SyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p><b>LiveBillManager Description:</b> 实体化直播节目业务类</p>
 *
 * @author douzi
 * <b>DATE</b> 2020年11月13日 下午4:32:41
 */
@Slf4j
@Component
public class LiveBillManager extends ParentManager {
	
    @Autowired
    MainAssertManager mainAssertManager;
	
	public void liveBillProcess(Content c, SyncTask st, ProductInfoPackage pip) {
		switch (c.getStatus().intValue()) {
	        case Constants.ASSET_STATUS_0:
	        case Constants.ASSET_STATUS_1:
	        case Constants.ASSET_STATUS_3:
	            liveBillUpdateProcess(c, st, pip);
	            break;
	        case Constants.ASSET_STATUS_2:
	            liveBillDelProcess(st);
	            break;
	        default:
	        	log.info("直播节目主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() +  "|不属于任何一种媒资状态:" + c.getStatus());
	            break;
	    }
	}

	private void liveBillDelProcess(SyncTask st) {
		mainAssertManager.mainDelProcess(st, "main-live");
	}

	private void liveBillUpdateProcess(Content c, SyncTask st, ProductInfoPackage pip) {
		List<PrdCont> dbPPcs = prdContService.findPcByContentId(c.getLiveContentId(), pip.getProductInfoPackageId());
		if (dbPPcs == null || dbPPcs.size() <= 0) {
			log.info("直播节目主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() + "|直播频道对应节目不存在:" + c.getLiveContentId() + "|二级产品：" + pip.getProductInfoPackageId());
			return;
		}
		
    	if (dbPPcs.size() == 1) {
    		pcProcess(c, pip, dbPPcs);
    	} else {
    		//TODO 去重直播频道后在入包    暂时不管
    		log.error("直播节目主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() + "|直播频道对应节目存在多个:" + c.getLiveContentId() + "|个数：" + dbPPcs.size());
    	}
	}

	/**
	 * <p><b>Title:</b> pcProcess</p>
	 * <p><b>Description:</b> 直播节目根据数据库是否存在走后续流程，直播节目不需要播控，走定时直接发布</p>
	 * @author douzi
	 * @param c
	 * @param pip
	 * @param dbPPcs
	 */
	private void pcProcess(Content c, ProductInfoPackage pip, List<PrdCont> dbPPcs) {
		PrdCont parentPc = dbPPcs.get(0);
		PrdInfo prdInfo = prdInfoService.get(parentPc.getPrdInfoId());
		if (prdInfo != null && prdInfo.getIsPublish() == 2 && 
				(prdInfo.getPrdInfoType() == null || prdInfo.getPrdInfoType() != 3)) {
			List<PrdCont> prdContDbs = prdContService.getPrdContByMmsId(c.getAssetId(), pip.getProductInfoPackageId());
	        if (prdContDbs.size() <= 0) {
	        	newLive(c, parentPc);
	        } else if (prdContDbs.size() == 1) {
	        	PrdCont pcDb = prdContDbs.get(0);
	        	// 比较数据库中的内容是否和传入内容相同，不同需要走版权pk业务
	        	if (pcDb.getContentId().equals(c.getContentId())) {
	        		updateLive(c, parentPc, prdContDbs);
	        	} else {
	        		findOptimalPrdCont(c, parentPc, prdInfo, prdContDbs, pcDb, "主流程-直播节目");
	        	}
	        } else {
				//TODO 去重直播节目后在入包   暂时不管
				log.error("直播节目流程|媒资:" + c.getAssetId() + "|内容:" + c.getContentId() + "|直播节目对应节目存在多个:" + c.getLiveContentId() + "|个数：" + dbPPcs.size());
			}
		} else {
			log.error("直播节目流程|媒资:" + c.getAssetId() + "|内容:" + c.getContentId() + "|直播频道对应的包为待发布状态或V+类型，不能发布");
		}
	}
}
